<?php

namespace app\backend\controller;

use app\backend\model\Category as Dao;
use think\Exception;
use think\Loader;
use think\Request;

class Category extends Common
{
    protected $Dao;
    protected $category = [];

    public function __construct(Request $request, Dao $Dao)
    {
        parent::__construct($request);

        $this->Dao = $Dao;

        $this->category = $this->Dao->select();
        $tree = make_option_tree_for_select($this->category, 0);
        $this->assign('category', $tree);
    }

    public function index(Request $request)
    {
        $vars = $request->param();
        if (!empty($vars['search_name']))
        {
            $this->map['category_name'] = ['like', "%{$vars['search_name']}%"];
        }

        try {
            $lists = $this->Dao->where($this->map)->paginate($this->pageSize, false, ['query'=>$this->map]);

        } catch (Exception $e) {
            echo $e->getMessage();
            exit;
        }

        $this->assign('categorys', $lists);
        $this->assign('map', $vars);
        $this->assign('page', $lists->render());

        return $this->fetch();
    }

    /**
     * @param Request $request
     * @return array
     */
    public function create(Request $request)
    {
        $vars = $request->param();
        $res = $this->validate($vars, 'Category.create');
        if (true !== $res) {
            return ['status' => 0, 'msg' => $res];
        }

        $category = $this->Dao->allowField('category_id')->create($vars);

        if ($category !== false) {
            return ['status' => 1, 'msg' => 'succ'];
        }
            return ['status' => 0, 'msg' => 'faild'];
    }

    /**
     * @param Request $request
     * @return array
     */
    public function delete(Request $request)
    {
        $vars = $request->param();
        $this->Dao->destroy($vars['category_id']);
        if (empty($vars['category_id'])) {
            return ['status' => 0, 'msg' => '缺少参数admin_id'];
        }
        return ['status' => 1, 'msg' => 'succ'];
    }

    /**
     * @param Request $request
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function read(Request $request)
    {
        $vars = $request->param();
        $res = $this->validate($vars,'Category.read');

        if (true !== $res) {
            return ['status' => 0, 'msg' => $res];
        }

        $data = $this->Dao->where('category_id', $vars['category_id'])->find();

        $category = $this->Dao->select();
        $tree = make_option_tree_for_select($category, 0, $data['pid']);
        $data['tree'] = $tree;

        if ($request->isAjax()) {
            return ['status' => 1, 'msg' => 'succ', 'data' => $data];
        } else {
            $this->assign('data', $data);
            $this->assign('category', $tree);
            return $this->fetch();
        }
    }

    /**
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $vars = $request->param();

        $res = $this->validate($vars, 'Category.update');

        if (true !== $res) {
            return ['status' => 0, 'msg' => $res];
        }

        //根据前台传来信息，更新到数据库中
        $data = $this->Dao->update($vars, [$this->Dao->getPk() => $vars[$this->Dao->getPk()]]);

        $this->assign('data', $data);
        return ['status' => 1, 'msg' => 'succ'];
    }
}